package com.yuelife.market.service.impl;

import com.yuelife.market.dao.MProductUserLikeDao;
import com.yuelife.market.entity.MProductUserLike;
import com.yuelife.market.service.MProductUserLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * (MProductUserLike)表服务实现类
 *
 * @author makejava
 * @since 2020-06-12 13:27:02
 */
@Service
public class MProductUserLikeServiceImpl implements MProductUserLikeService {
    @Autowired
    private MProductUserLikeDao mProductUserLikeDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public MProductUserLike queryById(Integer id) {
        return this.mProductUserLikeDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<MProductUserLike> queryAllByLimit(int offset, int limit) {
        return this.mProductUserLikeDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param mProductUserLike 实例对象
     * @return 实例对象
     */
    @Override
    public MProductUserLike insert(MProductUserLike mProductUserLike) {
        Date d=new Date();
        //时间格式
        SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String da=timeFormat.format(d);
        mProductUserLike.setLikeCreateTime(da);
        this.mProductUserLikeDao.insert(mProductUserLike);
        return mProductUserLike;
    }

    /**
     * 修改数据
     *
     * @param mProductUserLike 实例对象
     * @return 实例对象
     */
    @Override
    public MProductUserLike update(MProductUserLike mProductUserLike) {
        this.mProductUserLikeDao.update(mProductUserLike);
        return this.queryById(mProductUserLike.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.mProductUserLikeDao.deleteById(id) > 0;
    }

    /**
     * 初始化是否收藏，就是商品详情页的时候，收藏是否已被用户收藏或点赞过
     * @param productSn
     * @param userSn
     * @return
     */
    @Override
    public boolean selectFavorite(String productSn, String userSn) {
        MProductUserLike m=mProductUserLikeDao.selectFavorite(productSn,userSn);
        //如果该用户有收藏过，那么就初始化收藏被选中状态
        if (m!=null){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public void deleteFavorite(MProductUserLike mProductUserLike) {
        mProductUserLikeDao.deleteFavorite(mProductUserLike);
    }
}